package com.easydev.mq.rabbit.pool;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.easydev.mq.exception.MessageException;
import com.easydev.mq.rabbit.RabbitMQConfig;
import com.rabbitmq.client.Channel;

/**
 * <Description> Channel连接池 支持多虚拟HOST的情况<br>
 * 
 * @author lx<br>
 * @version 1.0<br>
 * @CreateDate 2016年3月23日 <br>
 */
public class RabbitChannelPoolTool {

    private Map<String, RabbitChannelPool> pools;

    private static RabbitChannelPoolTool poolTool;

    private String defaultVirtualHost;

    private String virtualHosts;

    private RabbitChannelPoolTool() throws MessageException {
        pools = new HashMap<String, RabbitChannelPool>();
        defaultVirtualHost = RabbitMQConfig.VIRTUALHOST_DEFAULT;
        virtualHosts = RabbitMQConfig.VIRTUALHOSTS;
        String[] hosts = {};
        // 配置虚拟连接池组
        if (StringUtils.isNotBlank(virtualHosts)) {
            hosts = virtualHosts.split(",");
            for (String host : hosts) {
                if (StringUtils.isNotBlank(host)) {
                    pools.put(host, new RabbitChannelPool(host));
                }
            }
        }
        // 如果defaultVirtualHost未设置 则取hosts的第一个
        if (StringUtils.isBlank(defaultVirtualHost) && hosts.length > 0) {
            defaultVirtualHost = hosts[0];
        }
        // defaultVirtualHost virtualHosts 均未设置则取“/”
        if (StringUtils.isBlank(defaultVirtualHost)) {
            defaultVirtualHost = "/";
        }
        if (pools.size() == 0 || (null == pools.get(defaultVirtualHost))) {
            pools.put(defaultVirtualHost, new RabbitChannelPool(defaultVirtualHost));
        }
    }

    /**
     * Description: RabbitChannelPoolTool单例方法<br>
     * 
     * @return instance
     * @throws MessageException
     */
    public static synchronized RabbitChannelPoolTool getInstance() throws MessageException {
        if (poolTool == null) {
            poolTool = new RabbitChannelPoolTool();
        }
        return poolTool;
    }

    /**
     * Description: 根据virtualHost获取相应的连接池，不传则为默认<br>
     * 
     * @author lx<br>
     * @taskId <br>
     * @param virtualHost rabbit virtualHost
     * @return
     * @throws Exception <br>
     */
    public Channel borrowObject(String virtualHost) throws Exception {
        Channel channel = null;
        if (StringUtils.isBlank(virtualHost)) {
            virtualHost = defaultVirtualHost;
        }
        RabbitChannelPool pool = pools.get(virtualHost);
        if (null != pool) {
            channel = pool.borrowObject();
        }
        return channel;
    }

    /**
     * Description: 根据virtualHost归还连接池中的CHANNEL，不传则为默认<br>
     * 
     * @author lx<br>
     * @param virtualHost rabbit virtualHost
     * @param channel
     * @throws Exception <br>
     */
    public void returnObject(String virtualHost, Channel channel) throws Exception {
        if (StringUtils.isBlank(virtualHost)) {
            virtualHost = defaultVirtualHost;
        }
        RabbitChannelPool pool = pools.get(virtualHost);
        if (null != pool) {
            pool.returnObject(channel);
        }
    }

}
